package com.mjw.zstu.controller;

import com.mjw.zstu.entity.Admin;
import com.mjw.zstu.entity.User;
import com.mjw.zstu.resultDto.JsonResult;
import com.mjw.zstu.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@CrossOrigin
@RequestMapping("admin")
public class AdminController {
    @Autowired
    AdminService adminService;
    @PostMapping("login")
    public JsonResult<Admin> login (@RequestBody Admin admin){
        Admin admin1=adminService.login(admin);
        if(admin1!=null){
            admin1.setPassword("");
            return new JsonResult(200,"登录成功",admin1);
        }

        return new JsonResult(500,"用户不存在",null);
    }
    @RequestMapping("list")
    public JsonResult list(){
        return adminService.list();
    }
    @RequestMapping("edit")
    public JsonResult edit(@RequestBody Admin admin){
        return adminService.edit(admin);
    }
    @RequestMapping("add")
    public JsonResult add(@RequestBody Admin admin){
        admin.setIs_admin(0);
        return adminService.add(admin);
    }
    @RequestMapping("delete")
    public JsonResult delete(int id){
        return adminService.delete(id);
    }
}
